using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Cysharp.Threading.Tasks;
using Light.Assets.Scripts.EventManager;
using Refresh;
using UnityEngine;
using UnityEngine.UI;

namespace Light
{
    public class SkillsJsonData
    {
        /// <summary>
        ///     ��ScriptObject�б�ǵ�Id
        /// </summary>
        public string id;

        /// <summary>
        ///     ��ʾ�������ڲ�
        /// </summary>
        public int index = 0;

        /// <summary>
        ///     ʹ��guid���ɵ�Ψһid
        /// </summary>
        public string uId;
    }

    public class SkillsJsonListData
    {
        public List<SkillsJsonData> data = new();
    }

    public class SkillsController : BasePanel
    {
        public SkillDetail skillChangeUI;

        private readonly Dictionary<ItemType, GameObject> SkillBarDec = new();

        private readonly Dictionary<AbstractItemSO, GameObject> SkillDic = new();

        private readonly List<Vector3> skillPosition = new();

        private GameObject contentUI;
        private int index;

        private GameObject skillPrefab;

        private GameObject skillViewPrefab;

        private void Awake()
        {
            contentUI = transform.Find("RightBar").gameObject;
            skillChangeUI = transform.Find("LeftBar/SkillDetails").GetComponent<SkillDetail>();
            skillPosition.Add(new Vector3(-200, 240, 0));
            skillPosition.Add(new Vector3(210, 240, 0));
            skillPosition.Add(new Vector3(-200, -250, 0));
            skillPosition.Add(new Vector3(210, -250, 0));
        }

        public override async void Init()
        {
            await LoadPrefabByAddressAbleAsync();

            CreateSkillViewObject();

            var evt = StatusBarEvent.Get();
            evt.BarType = FunctionBarType.Skill;
            evt.BarOperationType = FunctionBarOperationType.Init;

            //�������򻮷�
            //Ҫ�����е�����ȫ���������
            evt.contentTypes = new List<FunctionBarContentType> { FunctionBarContentType.SkillsBarView };

            EventManager.SendEvent(evt);
        }

        private async Task LoadPrefabByAddressAbleAsync()
        {
            ResManager.Instance.LoadAsync<GameObject>("Skill", gameobject => { skillPrefab = gameobject; },
                LoadType.AddressAbles);
            ResManager.Instance.LoadAsync<GameObject>("skillTree", gameobject => { skillViewPrefab = gameobject; },
                LoadType.AddressAbles);

            await UniTask.WaitUntil(() => skillPrefab != null);
        }

        private void CreateSkillViewObject()
        {
            foreach (ItemType item in Enum.GetValues(typeof(ItemType)))
                if (item == ItemType.Skill)
                {
                    var go = Instantiate(skillViewPrefab, contentUI.transform);

                    go.GetComponentInChildren<Text>().text = "�츳";

                    //��ʼ������Ĭ��ȫ��false
                    go.SetActive(false);

                    SkillBarDec.Add(item, go);
                }
        }

        public override void Refresh<T>(T info)
        {
            base.Refresh(info);

            var data = info as StatusBarInfo;

            var i = 1;
            foreach (var item in data.operations)
                if (item.Item2 == FunctionBarOperationType.Add)
                {
                    ItemAdd(data, item, i);
                    ++i;
                }

            index = 0;
        }

        private void ItemAdd(StatusBarInfo data, (FunctionBarData, FunctionBarOperationType) operation, int i)
        {
            var temp = operation.Item1 as SkillsBarData;
            //׼��ʵ��������Ϸ����
            GameObject go = null;

            GameObject parentGo = null;

            //��ѡ���ʵ�item������
            SkillBarDec.TryGetValue(temp.type, out parentGo);

            parentGo.SetActive(true);

            SkillDic.TryGetValue(temp.data, out go);
            if (go != null)
            {
                go.transform.SetParent(parentGo.transform);
                var tempSkill = go.GetComponent<Skill>();
                tempSkill.Init(this);
                return;
            }

            //ʵ����Item��Ϸ����
            go = Instantiate(skillPrefab, parentGo.transform);
            if (i != 3)
            {
                go.transform.localPosition = skillPosition[index];
                ++index;
            }

            SkillDic.TryAdd(temp.data, go);

            //Skill����ע��

            //����Panel������Ϊ���������ܵĵ��÷�ʽ
            var tempItemOut = go.GetComponentInChildren<Skill>();

            tempItemOut.Init(this);

            //��ÿһ��skill��������
            var evt = ItemRefreshData.Get();

            evt.type = FunctionBarOperationType.Add;
            evt.data = temp.data;

            //TODO��������Ҫ�������
            go.GetComponentInChildren<IRefresh>().Refresh(evt);
        }

        public override void ReOpen()
        {
        }
    }
}